package com.tuanzi.loan.business.analyzer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tuanzi.loan.vo.view.family.UserValidateView.PhoneContact;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户通讯录JSON数据解析
 */
@Slf4j
@Component
public class PhoneContactAnalyzer {
	/**
	 * 
	 * @param jsonStr
	 * @param isFilter 是否过滤敏感信息
	 * @return
	 */
    public List<PhoneContact> getPhoneContact(String jsonStr, boolean isFilter){
        List<PhoneContact> contacts = new ArrayList<>();
        if (StringUtils.isNotBlank(jsonStr)){
            try {
                JSONArray array = JSON.parseArray(jsonStr);
                for (int i = 0; i < array.size(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    JSONArray phoneArray = object.getJSONArray("phone");
                    for (int j = 0; j < phoneArray.size(); j++) {
                        PhoneContact contact = new PhoneContact();
                        String name = object.getString("name");
                        String number = phoneArray.getString(j);
                        if(isFilter) {
                        	name = PhoneNumNideUtil.hideName(name);
                        	number = PhoneNumNideUtil.hideNum(number);
                        }
                    	contact.setName(name);
                        contact.setNumber(number);
                        contacts.add(contact);
                    }
                }
            }
            catch (Exception e){
                return contacts;
            }
        }
        return contacts;
    }
}
